package com.example.pei.ithinklearnt.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.pei.ithinklearnt.R;

/**
 * Created by Pei on 2017/12/18.
 */

public class DialogUtil {
    public static void CreateVerifyDialog(Context context, String message, final CreateVerifyDialogListener listener){
        AlertDialog alert = new AlertDialog.Builder(context).create();
        alert.setMessage(message);
        //添加取消按钮
        alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.cancel();
            }
        });
        //添加"确定"按钮
        alert.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                listener.confirm();
            }
        });
        alert.show();
    }
    public interface CreateVerifyDialogListener{
        void confirm();
        void cancel();
    }



    private static PopupWindow popupWindow;
    public static void  showSmallDeleteDialog(Context context, View view, final SmallDeleteDialogListener listener){
        View popView = LayoutInflater.from(context).inflate(R.layout.layout_long_click_dialog, null);
        TextView tvDelete=  popView.findViewById(R.id.tv_delete);
        if(null==popupWindow){
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.click();
                    popupWindow.dismiss();
                    popupWindow=null;

                }
            });
            popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setAnimationStyle(R.style.PopAnimStyle);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
        }
        if (popupWindow.isShowing()){
            popupWindow.dismiss();
            popupWindow=null;

        }

        //第一次显示控件的时候宽高会为0
        int deleteHeight=tvDelete.getHeight()==0?145:tvDelete.getHeight();
        int deleteWidth=tvDelete.getWidth()==0?212:tvDelete.getWidth();

        popupWindow.showAsDropDown(view,(view.getWidth()-deleteWidth)/2,-view.getHeight()-deleteHeight);
    }

    public static void hideSmallDeleteDialog(){
        if(popupWindow!=null){
            popupWindow.dismiss();
            popupWindow=null;
        }
    }


    public interface SmallDeleteDialogListener{
        void click();
    }



}
